﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kona.Model;
using Kona.Infrastructure;

namespace Kona.Web {
    public class KonaController:Controller {

        IObjectStore _objectStore;
        const int PAGE_SIZE = 20;
        ICustomerRepository _customerRepository;
        ICMSRepository _cmsRepository;
        string _thisUserName;
        public KonaController() { }
        public KonaController(ICustomerRepository customerRepository, IObjectStore objectStore, ICMSRepository cmsRepository) {

            _customerRepository = customerRepository;
            _cmsRepository = cmsRepository;
            _objectStore = objectStore;

            SetSiteData();
            SetCustomer();
            SetPages();
            //Messaging API sample code
            //this.AddErrorMessage("Error: authentication failed for PayPal plugin");
            //this.AddWarningMessage("Warning: some warning");
            //this.AddWarningMessage("Warning: some warning 2");
            //this.AddInformationMessage("Information: some information");
            //this.RemoveMessage("Warning: some warning 2");
            //this.DisplayMessages();
        }

        void SetCustomer() {
            _thisUserName = this.GetCommerceUserName();

            var customer = _customerRepository.GetCustomer(_thisUserName);
            if (customer == null) {
                customer = new Customer();
                customer.UserName = _thisUserName;
                
                customer.LastName = "";
                customer.Email = "";
                customer.LanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
                customer.FirstName = "Guest";
                //save em
                //_customerRepository.AddCustomer(customer);
                this.RememberCommerceUser(_thisUserName);
            }
            this.CurrentCustomer = customer;
            this.CurrentCart = customer.Cart;
        }

        void SetSiteData() {
            string storeUrl = Url.GetSiteUrl();
            
            //HACK: hard-coding this for demo, will fix in a few days
            //var result = store.Get<KonaSite>("Site", storeUrl);
            KonaSite result = null;
            _objectStore.Delete("Site", storeUrl);
            if (result == null) {
                result = new KonaSite();
                result.AdminEmail = "rob@wekeroad.com";
                result.Description = "Fuji Test Site";
                result.IsPublic = true;
                result.LanguageCode = "en";
                result.Owner = "admin";
                result.OwnerName = "Microsoft Corporation";
                result.SiteHost = "localhost";
                result.SiteName = "Kona Demo Site";
                result.SiteUrl = "http://localhost:41854/";
                result.SiteID = Guid.NewGuid();
                result.TemplateName = "Blue";
                result.RPXAPIKey = "GET_A_KEY_FROM_RPXNOW";
                result.RPXNowUrl = "https://localhost18622.rpxnow.com/openid/embed";
                result.AddressForm = "USDefault";
                result.AcceptCreditCards = true;
                result.AcceptPayPal = true;
                result.AcceptPOs = false;
                result.PayPalPDTToken = "GET_THIS_FROM_PAYPAL";
                result.CreditCardForm = "DefaultCreditCard";
                result.PayPalForm = "DefaultPayPalStandard";
                result.CurrencyCode = "USD";
                result.PayPalBusinessEmail = "someone@example.com";
                result.OrderDetailView = "DefaultItems";
                result.OrderHeaderView = "DefaultHeader";

                result.SMTPLogin = "MY Secured Login :)";
                result.SMTPPassword = "Can't See Me";
                result.SMTPServer = "Myserver.com";
                result.TaxPlugin = "USTaxCalculator";
                result.ShippingPlugin = "SimpleShippingCalculator";
                result.CreditCardProcessor = "FakeProcessor";

                //set it
                _objectStore.Store<KonaSite>("Site", storeUrl, result);
            }
            this.ThemeName = result.TemplateName;
            SiteData = result;

        }

        void SetPages() {
            this.SitePages = _cmsRepository.GetPages(PublishStatus.Published) ?? new List<Page>();
        }

        string _themeName;
        public string ThemeName {
            get {
                if (String.IsNullOrEmpty(_themeName))
                    _themeName = "Default";
                return _themeName;
            }
            set {
                _themeName = value;

            }
        }
        public void SaveCart() {
            _customerRepository.SaveCart(this.CurrentCart);
        }


        IList<ShippingMethod> _shippingMethods;
        public IList<ShippingMethod> ShippingMethods {
            get {
                return _shippingMethods;
            }
            set {
                _shippingMethods = value;
                ViewData["ShippingMethods"] = _shippingMethods;
            }
        }

        public KonaSite SiteData {
            get {

                return (KonaSite)ViewData["SiteData"];
            }
            set {
                ViewData["SiteData"] = value;
            }
        }

        public IList<Page> SitePages {
            get {

                return (IList<Page>)ViewData["SitePages"];
            }
            set {
                ViewData["SitePages"] = value;
            }
        }
        public ShoppingCart CurrentCart {
            get {

                return (ShoppingCart)ViewData["CurrentCart"];
            }
            set {
                ViewData["CurrentCart"] = value;
            }
        }
        
        public Customer CurrentCustomer {
            get {
                return (Customer)ViewData["CurrentCustomer"];
            }
            set {
                ViewData["CurrentCustomer"] = value;
            }
        }
        public Page CurrentPage {
            get {
                return (Page)ViewData["CurrentPage"];
            }
            set {
                ViewData["CurrentPage"] = value;
            }
        }

        IList<Product> _products;
        public IList<Product> Products {
            get {
                return _products;
            }
            set {
                _products = value;
                ViewData["Products"] = _products;
            }
        }

        Product _product;
        public Product SelectedProduct {
            get {
                return _product;
            }
            set {
                _product = value;
                ViewData["SelectedProduct"] = _product;
            }
        
        }

    }
}
